﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using NewLife.Web;
using NewsPub;
using XCode;

public partial class List : MyPageBase
{
    /// <summary>编号</summary>
    public Int32 EntityID { get { if (WebHelper.RequestInt("id") == 0)Response.Redirect("Default.aspx"); return WebHelper.RequestInt("id"); } }

    private Classis _Entity;
    /// <summary>客户</summary>
    public Classis Entity
    {
        get { return _Entity ?? (_Entity = Classis.FindByID(EntityID)); }
        set { _Entity = value; }
    }
    int pageSize = 20;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            if (Entity.ID == 0) Response.Redirect("Default.aspx");
            //HtmlGenericControl Title = new HtmlGenericControl();
            this.Master.Page.Title = Entity.Name + "-尤溪县环境保护局";
            DataBind();

            rptleftmenusub.DataSource = Classis.FindByID(Entity.Childs.Count == 0 ? Entity.ParentID : Entity.ID).AllChilds.FindAll("IsShow", true);
            rptleftmenusub.DataBind();
            //int sid = WebHelper.RequestInt("sid"); int id = WebHelper.RequestInt("id");
            EntityList<Details> delist = null;
            //delist = Details.FindALLByClassisID(Entity.ID);
            //delist=Details.FindAll("ClassisId", Entity.ID, 0, 5);
            //delist = Details.FindAll("select * from details where classisid in (select id from classis where parentid=" + EntityID + " or id=" + EntityID + ") limit 0,5");
            delist = Details.SearchList(EntityID.ToString(), "CreateTime desc", 0, pageSize);
            //按创建时间反排序
            //delist.Sort("CreateTime", true);
            
            rptlist.DataSource = delist;
            rptlist.DataBind();
            pager.PageSize = pageSize;
            pager.TotalRowCount = Details.SearchListCount(EntityID.ToString(), "CreateTime DESC", 0, 0);
            if (pager.TotalRowCount <= pageSize) pager.Visible = false;
            //else { delist = Details.FindAll("select * from details where classisid=" + id); delist.Sort("CreateTime", true); rptlist.DataSource = delist; rptlist.DataBind(); }
            //rptcategorylist.DataSource
            //Classis.FindAllByParent
        }
    }
    protected void rptleftmenu_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        Repeater dlproduct = (Repeater)e.Item.FindControl("rptleftmenusub");
        //DataRowView rowv = (DataRowView)e.Item.DataItem;
        Classis rowv = (Classis)e.Item.DataItem;
        int CategoryId = Convert.ToInt32(rowv["ID"]);
        Classis classis = new Classis();
        dlproduct.DataSource = Classis.FindAllByParentID(CategoryId).ToDataTable(false);
        dlproduct.DataBind();
    }
    protected void DataPager2_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        Search(e.NewPageIndex);
    }
    void Search(Int32 index)
    {


        // 总行数

        pager.TotalRowCount = Details.SearchListCount(EntityID.ToString(), "CreateTime DESC", 0, 0);
        pager.PageSize = pageSize;

        //String tableName = ddlTable.SelectedValue;
        //String fsql = String.Format("Select * From {0}", dal.Db.FormatKeyWord(tableName));
        //if (sql.ToLower().StartsWith(fsql.ToLower()))
        //{
        //    IEntityOperate factory = dal.CreateOperate(tableName);
        //    if (factory != null)
        //    {
        //        gvResult.DataSource = factory.FindAll(sql.Substring(fsql.Length), null, null, index * gvResult.PageSize, gvResult.PageSize);
        //        gvResult.DataBind();
        //        return;
        //    }
        //}

        //String key = txtKey.Text;
        //if (String.IsNullOrEmpty(key)) key = "ID";

        //sql = dal.PageSplit(sql, index * gvResult.PageSize, gvResult.PageSize, key);

        //DataSet ds = dal.Select(sql, "");
        //gvResult.DataSource = ds;
        //gvResult.DataBind();
        EntityList<Details> delist = null;
        delist = Details.SearchList(EntityID.ToString(), "CreateTime desc", index * pageSize, pageSize);
            //按创建时间反排序
            //delist.Sort("CreateTime", true);
            rptlist.DataSource = delist;
        rptlist.DataBind();
    }
}